package com.match.competition_apply.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.match.competition_apply.entity.CompetitionInfo;
import org.apache.poi.ss.usermodel.Workbook;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static com.match.competition_apply.utils.CompetitionExcelUtil.parseTimeToString;

public class FileUtil {
    public static void downloadFile(HttpServletResponse response, List<CompetitionInfo> competitionInfoList) {
//        List list = new ArrayList<Map<String, Object>>();
//        //数据准备
//        for (int i = 0; i <= 100; i++){
//            Map<String, Object> map = new HashMap<>();
//            map.put("name", "xkx");
//            map.put("A", i);
//            map.put("B", i);
//            map.put("C", i);
//            map.put("D", i);
//            list.add(map);
//        }

        List<Map<String, Object>> maps = transitionAdaptiveMap(competitionInfoList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("CompetitionList", maps);
        try {
            //读取模板
            TemplateExportParams params = new TemplateExportParams("F:\\DcProject_FX\\service\\service_competition\\src\\main\\resources\\CompetitionExcel.xlsx");
            //设置sheet名,以数组的形式,可多个
            params.setSheetName(new String[]{"测试sheet名"});



            Workbook workbook = ExcelExportUtil.exportExcel(params, map);
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取所有数据封装成mapList
     *
     */
    public static  List<Map<String, Object>> transitionAdaptiveMap(List<CompetitionInfo> competitionInfoList) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (CompetitionInfo competitionInfo : competitionInfoList) {
            Map<String, Object> mapOne = new HashMap<>();
            mapOne.put("competitionBegin", parseTimeToString(competitionInfo.getCompetitionBegin()));
            mapOne.put("competitionEnd",parseTimeToString(competitionInfo.getCompetitionEnd()));
            mapOne.put("id", competitionInfo.getId());
            mapOne.put("competitionName", competitionInfo.getCompetitionName());
            mapOne.put("entryLever", competitionInfo.getEntryLever());
            mapOne.put("competitionLever", competitionInfo.getCompetitionLever());
            mapOne.put("competitionType", competitionInfo.getCompetitionType());
            mapOne.put("directoryInOut", competitionInfo.getDirectoryInOut());
            mapOne.put("newAdd", competitionInfo.getNewAdd());
            mapOne.put("rewardsBureau", competitionInfo.getRewardsBureau());
            mapOne.put("competitionDepartment", competitionInfo.getCompetitionDepartment());
            mapOne.put("competitionOrganizer", competitionInfo.getCompetitionOrganizer());
            mapOne.put("replaceCredits", competitionInfo.getReplaceCredits());
            mapOne.put("replaceCourse", competitionInfo.getReplaceCourse());
            mapOne.put("queueCount", competitionInfo.getQueueCount());
            mapOne.put("queuePersonCount", competitionInfo.getQueuePersonCount());
            mapList.add(mapOne);
        }
        return mapList;
    }
    /**
     * export导出请求头设置
     *
     * @param response
     * @param workbook
     * @param fileName
     * @throws Exception
     */
    private static void export(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
        response.reset();
        response.setContentType("application/x-msdownload");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "filename=" + java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
        ServletOutputStream outStream = null;
        try {
            outStream = response.getOutputStream();
            workbook.write(outStream);
        } finally {
            outStream.close();
        }
    }
}
